<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {updateUserPwdService} from '@/api/user.js'
import { useRouter } from 'vue-router'
const router = useRouter()
const userPwd=ref({
    "old_pwd":'',
    "new_pwd":'',
    "re_pwd":''
})
const rules = {
    //输入的新密码必须满足5到16个连续非空白字符的字符串
    old_pwd: [
        { required: true, message: '请输入原密码', trigger: 'blur' },
        {
            pattern: /^\S{5,10}$/,
            message: '密码必须是5到16个连续非空白字符的字符串!',
            trigger: 'blur'
        }
    ],
    new_pwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        {
            pattern: /^\S{5,10}$/,
            message: '密码必须是5到16个连续非空白字符的字符串!',
            trigger: 'blur'
        }
    ],
    re_pwd: [
        { required: true, message: '请确认原密码', trigger: 'blur' },
        {
            pattern: /^\S{5,10}$/,
            message: '密码必须是5到16个连续非空白字符的字符串!',
            trigger: 'blur'
        }
    ]
}

//重置密码
const updateUserPwd=async()=>{
   let result = await updateUserPwdService(userPwd.value)
   ElMessage.success(result.message?'密码重置成功!':result.message)

   //更新成功后跳转至登录页面
    router.push('/login')

}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userPwd" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="old_pwd">
                        <el-input type="password" v-model="userPwd.old_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input type="password" v-model="userPwd.new_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="re_pwd">
                        <el-input type="password" v-model="userPwd.re_pwd"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserPwd">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>